package com.orphan.marketFront.controller;


import com.orphan.marketFront.mapper.UserInfoMapper;
import com.orphan.marketFront.po.UserInfo;
import com.orphan.marketFront.service.IUserDetailService;
import com.orphan.marketFront.service.impl.UserDetailServiceImpl;
import com.orphan.marketFront.util.Base64ToStream;
import com.orphan.marketFront.util.FileUtil;
import com.orphan.marketFront.util.StringUtil;
import com.orphan.util.dto.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lucangtian
 * @since 2021-07-16
 */
@Api(tags = "用户详情接口")
@RestController
@RequestMapping("/user-detail")
public class UserDetailController {

    @Autowired
    IUserDetailService userDetailService;

    @Autowired
    UserInfoMapper userDtoMapper;

    @ApiOperation(value = "用户资料补齐", notes = "用户资料补齐")
    @PostMapping("/addUserDetail")
    public Map<String, Object> addUserDetail(@RequestParam("uId") Long uId,
                                             @RequestParam("company") String company,
                                             @RequestParam("identityImage") MultipartFile identityImage,
                                             @RequestParam("licenseImage") MultipartFile licenseImage){

        Map<String, Object> map = new HashMap<String, Object>();

        //保存到服务器硬盘
        String identityAddress = FileUtil.uploadFile(identityImage);
        String licenseAddress = FileUtil.uploadFile(licenseImage);

        //保存到数据库
        int save = userDetailService.save(uId, company, identityAddress, licenseAddress);
//        System.out.println(uId);
//        System.out.println(company);
//        System.out.println(identityAddress);
//        System.out.println(licenseAddress);
//        System.out.println(save);
        if(save==0){
            map.put("msg", StringUtil.UPLOADFILE_REPEAT);
            return map;
        }

        //获取base64编码
        String identityBase64 = Base64ToStream.ImageToBase64(StringUtil.IMG_NAME+ identityAddress);
        String licenseBase64 = Base64ToStream.ImageToBase64(StringUtil.IMG_NAME+ licenseAddress);

        //封装传输数据
        UserInfo userInfo = userDtoMapper.getUserById(3l);
        userInfo.setIdentity(identityBase64);
        userInfo.setLicense(licenseBase64);
        UserDto userDto=new UserDto();
        BeanUtils.copyProperties(userInfo, userDto);

        //使用mq发送到审核系统
        //todo
//        userDetailService.sendUser(userDto);

        map.put("msg", StringUtil.UPLOADFILE_SUCCESS);
        return map;

    }
}
